﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.App;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Util;
using Kingdee.K3.FIN.CB.App.Report;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS;

namespace RK.ReportPlugin.BPW
{
    [Description("简单账表：完工入库数量查询报表实现_BPW扩展"), HotUpdate]
    public class CompletionQtyRptExtend : CompletionQtyRpt
    {
        private String[] customRptTempTableNames;
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            //base.BuilderReportSqlAndTempTable(filter, tableName);
            IDBService dBService = ServiceHelper.GetService<IDBService>();
            customRptTempTableNames = dBService.CreateTemporaryTableName(this.Context, 1);
            String strTable = customRptTempTableNames[0];
            base.BuilderReportSqlAndTempTable(filter, strTable);
            //对初步的查询结果进行处理，然后写回基类默认的存放查询结果的临时表
            StringBuilder sb = new StringBuilder();
            string strSql = "/*dialect*/select t1.*, a3.FPERUNITSTANDHOUR,A3.FPERUNITSTANDHOUR*T1.FQTYFIELD UnitSec into {0}  FROM {1} t1 left join T_BD_MATERIAL a2 on"
                           + "  t1.FMATERIALFIELD_FNUMBER = a2.FNUMBER "
                           + " left join t_BD_MaterialProduce a3 on a3.FMATERIALID=a2.FMATERIALID";
            //sql拼接注解：T_BD_CUSTOMER_L  客户名称表  FSHORTNAME  客户简称字段                                                                          
            //这里可以断点调试去找这两张表单的关联关系
            sb.AppendFormat(strSql, tableName, strTable);
            DBUtils.Execute(this.Context, sb.ToString());
        }
        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            ReportHeader header = base.GetReportHeaders(filter);
            // TODO: 添加新增字段
            header.AddChild("FPERUNITSTANDHOUR", new LocaleValue("标准工时"));
            header.AddChild("UnitSec", new LocaleValue("单位总工时"));
            return header;
        }
        public override void CloseReport()
        {
            //删除临时表
            if (customRptTempTableNames.IsNullOrEmptyOrWhiteSpace())
            {
                return;
            }
            IDBService dbService = Kingdee.BOS.App.ServiceHelper.GetService<Kingdee.BOS.Contracts.IDBService>();
            dbService.DeleteTemporaryTableName(this.Context, customRptTempTableNames);
            base.CloseReport();
        }
    }
}
